package com.action;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bean.ResponseBean;
import com.pojo.Admin;
import com.pojo.VpnPeizhi;
import com.pojo.Zhuji;
import com.service.VpnPeizhiService;
import com.util.IPUtil;
import com.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 国内外配置文件Action
 */
@RestController
@RequestMapping("/peizhi")
public class PeizhiWenjianAction {

    @Autowired
    private RedisTemplate redisTemplate;
    @Value("${guonei_url}")
    private String guonei_url;
    @Value("${guowai_url}")
    private String guowai_url;
    @Autowired
    private VpnPeizhiService vpnPeizhiService;

    /**
     * 读取国内外配置文件
     * @param type   0为国内，1为国外
     */
    @RequestMapping("/duqu_wenjian")
    public ResponseBean duqu_wenjian(String type) {

        if(StringUtil.isNull(type)){
            return new ResponseBean(1,1,"参数有误，无法获取！","");
        }
        String config = null;
        try {
            if(type.equals("0")){
                config=IPUtil.getguo_nei_wai(guonei_url);
            }else if(type.equals("1")){
                config=IPUtil.getguo_nei_wai(guowai_url);
            }
        }catch (Exception e) {
            return new ResponseBean(1,1,"文件有误！","");
        }
        if(config == null){
            return new ResponseBean(1,1,"请先提供配置文件！","");
        }
        return new ResponseBean(0,1,"获取成功！",config);
    }

    /**
     * 保存国内外配置文件
     * @param type   0为国内，1为国外
     * @param context  文件内容
     */
    @RequestMapping("/xie_wenjian")
    public ResponseBean xie_wenjian(String type,String context) {

        if(StringUtil.isNull(type)){
            return new ResponseBean(1,1,"参数有误，无法获取！","");
        }
        if(StringUtil.isNull(context)){
            return new ResponseBean(1,1,"写入内容不能为空！","");
        }
        String config = null;
        try {
            if(type.equals("0")){
                config=IPUtil.setguo_nei_wai(guonei_url,context);
                redisTemplate.delete("guonei_ip_config");
            }else if(type.equals("1")){
                config=IPUtil.setguo_nei_wai(guowai_url,context);
                redisTemplate.delete("guowai_ip_config");
            }
        }catch (Exception e) {
            return new ResponseBean(1,1,"写入失败","");
        }
        return new ResponseBean(0,1,"写入成功！",config);
    }

    /**
     * 分页查询IP和域名
     * @param name  关键字
     * @param page  页面
     * @param limit  条数
     * @return
     */
    @RequestMapping("/selectIP_yuming")
    public ResponseBean selectIP_yuming(String name, @RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit){
        IPage iPage = vpnPeizhiService.selectIP_yuming(page,limit,name);
        return new ResponseBean(0,iPage.getTotal(),"查询成功",iPage.getRecords());
    }
    /**
     * 新增IP和域名
     * @param  vpnPeizhi   IP和域名
     * @return
     */
    @RequestMapping("/addYumingIp")
    public ResponseBean addYumingIp(@RequestBody VpnPeizhi vpnPeizhi, HttpServletRequest request){

        if(StringUtil.isNull(vpnPeizhi.getIp_address())){
            return new ResponseBean(1,1,"IP地址不能为空","");
        }
        if(StringUtil.isNull(vpnPeizhi.getYu_ming())){
            return new ResponseBean(1,1,"域名不能为空","");
        }
        if(vpnPeizhi.getType() == null){
            vpnPeizhi.setType(0);
        }
        if(vpnPeizhi.getPd_type()==null){
            vpnPeizhi.setPd_type(1);
        }
        if(vpnPeizhiService.save(vpnPeizhi)){
            return new ResponseBean(0,1,"操作成功","");
        }
        return new ResponseBean(1,1,"操作失败","");
    }

    /**
     * 修改IP和域名
     * @param vpnPeizhi   IP和域名
     * @return
     */
    @RequestMapping("/updYumingIp")
    public ResponseBean updYumingIp(@RequestBody VpnPeizhi vpnPeizhi) {

        VpnPeizhi vpnPeizhi2 = vpnPeizhiService.getById(vpnPeizhi.getId());
        if(vpnPeizhi2 == null){
            return new ResponseBean(1,1,"未找到该主机","");
        }
        if(StringUtil.isNull(vpnPeizhi.getIp_address())){
            return new ResponseBean(1,1,"IP地址不能为空","");
        }
        if(StringUtil.isNull(vpnPeizhi.getYu_ming())){
            return new ResponseBean(1,1,"域名不能为空","");
        }
        if (vpnPeizhiService.updateById(vpnPeizhi)) {
            return new ResponseBean(0, 1, "修改成功","");
        }
        return new ResponseBean(1, 1, "操作失败","");
    }

    /**
     * 删除IP和域名
     * @param id   IP和域名id
     */
    @RequestMapping("/deleteYumingIp")
    public ResponseBean deleteYumingIp(String id) {

        if(vpnPeizhiService.removeById(id)){
            return new ResponseBean(0,1,"删除成功");
        }
        return new ResponseBean(1,1,"删除失败");
    }
}
